package com.scisoft.scan.control.rent;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.scisoft.scan.control.ClearEditText;
import com.scisoft.scan.control.ScanHelper;
import com.scisoft.scan.data.BarCodeBean;
import com.scisoft.scan.data.PartBean;
import com.scisoft.scan.data.rent.RentBarcodeBean;
import com.scisoft.scan.data.rent.RentOperator;
import com.scisoft.scan.data.rent.RentOrderBean;
import com.scisoft.scan.data.rent.RentOrderDetailBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.SysParameter;
import com.scisoft.scan.ws.WebServiceHelper;
import com.scisoft.scan.ws.WebServiceHelper_Rent;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RentOrderDetailActivity extends Activity {

    private ImageView iv_Back; //返回按钮
    private ListView lv_data;
    private TextView bt_all;
    private TextView bt_undo;
    private TextView bt_done;
    private TextView tv_submit;
    private View v_all;
    private View v_undo;
    private View v_done;
    private ClearEditText et_bar;
    private List<RentOrderDetailBean> depotOrderDetailBeanList;
    private List<RentOrderDetailBean> currentList;
    private String currentflag = "1";//1 全部 2 待处理 3 已处理
    private RentOrderBean orderBean;
    private RentOrderDetailBean currentDetailBean;
    private String orderState;//0 未完成 1已完成

    //返回当前界面
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        QueryDepotOrder();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//即隐藏标题栏
        setContentView(R.layout.activity_rent_order_detail);

        //初始化扫码
        init();

        currentList = new ArrayList<>();
        depotOrderDetailBeanList = new ArrayList<>();
        //参数传递
        Intent intent = getIntent();
        orderBean = (RentOrderBean) intent.getSerializableExtra("data");
        TextView tv_orderType = findViewById(R.id.tv_orderType);
        tv_orderType.setText(orderBean.orderTypeName);
        TextView tv_title = findViewById(R.id.tv_title);
        tv_title.setText(orderBean.supplierName);
        TextView tv_orderNo = findViewById(R.id.tv_orderNo);
        tv_orderNo.setText(orderBean.orderNo);
        TextView tv_depotName = findViewById(R.id.tv_depotName);
        tv_depotName.setText(orderBean.depotName);
        //控件初始化
        lv_data = findViewById(R.id.lv_data);
        lv_data.setOnItemClickListener(new ListViewClick());
        tv_submit = findViewById(R.id.tv_submit);
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DEPOT_06();
            }
        });
        orderState = orderBean.orderState.contains("完成") ? "1" : "0";
        if (orderState.equals("1")) {
            tv_submit.setVisibility(View.INVISIBLE);
        } else {
            tv_submit.setVisibility(View.VISIBLE);
            //滑动
            setLeftScroll();
        }
        et_bar = findViewById(R.id.et_bar);
        bt_all = findViewById(R.id.bt_all);
        bt_undo = findViewById(R.id.bt_undo);
        bt_done = findViewById(R.id.bt_done);
        v_all = findViewById(R.id.v_all);
        v_undo = findViewById(R.id.v_undo);
        v_done = findViewById(R.id.v_done);
        //页签切换
        bt_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
                BindData(RentOrderDetailActivity.this);
            }
        });
        bt_undo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
                BindData(RentOrderDetailActivity.this);
            }
        });
        bt_done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setButton(v);
                BindData(RentOrderDetailActivity.this);
            }
        });
        setButton(bt_all);

        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //音频播放
        initSoundPool();
        //查询当前订单用户是否一致
        CommonUtil.showPress(this);
        QueryLastOperatorTask queryLastOperatorTask = new QueryLastOperatorTask(RentOrderDetailActivity.this);
        queryLastOperatorTask.execute();
    }

    //左滑事件处理
    private float startX;
    private float startY;
    private float downX;
    private float downY;

    @SuppressLint("ClickableViewAccessibility")
    private void setLeftScroll() {
        lv_data.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        startY = event.getY();
                        break;
                    case MotionEvent.ACTION_UP:
                        downX = event.getX();
                        downY = event.getY();
                        if (Math.abs(downY - startY) < Math.abs(downX - startX)) {
                            int position1 = lv_data.pointToPosition((int) startX, (int) startY);
                            int position2 = lv_data.pointToPosition((int) downX, (int) downY);
                            //从右向左鼠标移动超过20判断为左滑，并且每次滑动都在一行
                            if (startX - downX > 20 && position1 == position2)
                                //Toast.makeText(DepotDetailActivity.this, "左右" + position1, Toast.LENGTH_LONG).show();
                                if (position1 >= 0 && position1 < currentList.size()) {
                                    RentOrderDetailBean temp = currentList.get(position1);
                                    //只有无码件可以左滑
                                    if ("0".equals(temp.ifGyssm)) {
                                        currentDetailBean = temp;
                                        //注意这里是列表传地址引用，才可以这么写。
                                        if (temp.ifOk.equals("0")) {
                                            temp.ifOk = "1";
                                            temp.notPickedCount = "0";
                                            temp.pickedCount = temp.partCount;
                                            DEPOT_04();//更新当前已处理
                                        } else {
                                            temp.ifOk = "0";
                                            temp.notPickedCount = temp.partCount;
                                            temp.pickedCount = "0";
                                            DEPOT_05();//更新未处理
                                        }
                                        QueryDepotOrder();
                                    }
                                }
                            return true;
                        }
                        break;
                }
                return false;
            }
        });
    }

    //进入二级界面
    private class ListViewClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(RentOrderDetailActivity.this, RentOrderDetailScanActivity.class);
            if (position >= 0 && position < currentList.size()) {
                RentOrderDetailBean depotOrderDetailBean = currentList.get(position);
                //只有有码件可以进入录入上传二维码界面
                if ("1".equals(depotOrderDetailBean.ifGyssm)) {
                    intent.putExtra("orderId", orderBean.borrowId);
                    intent.putExtra("orderState", orderState);
                    intent.putExtra("data", depotOrderDetailBean);
                    startActivityForResult(intent, 0);
                }

            }
        }
    }

    //设置tab页的颜色
    private void setButton(View v) {
        if (v.getId() == bt_all.getId()) {
            currentflag = "1";
            v_all.setVisibility(View.VISIBLE);
            v_undo.setVisibility(View.INVISIBLE);
            v_done.setVisibility(View.INVISIBLE);
            bt_all.setTextColor(getResources().getColor(R.color.tab_text_color_select));
            bt_undo.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_done.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
        } else if (v.getId() == bt_undo.getId()) {
            currentflag = "2";
            v_all.setVisibility(View.INVISIBLE);
            v_undo.setVisibility(View.VISIBLE);
            v_done.setVisibility(View.INVISIBLE);
            bt_all.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_undo.setTextColor(getResources().getColor(R.color.tab_text_color_select));
            bt_done.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
        } else {
            currentflag = "3";
            v_all.setVisibility(View.INVISIBLE);
            v_undo.setVisibility(View.INVISIBLE);
            v_done.setVisibility(View.VISIBLE);
            bt_all.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_undo.setTextColor(getResources().getColor(R.color.tab_text_color_unselect));
            bt_done.setTextColor(getResources().getColor(R.color.tab_text_color_select));
        }
    }

    //绑定数据
    private void BindData(Context context) {
        //判断当前页签
        currentList.clear();
        if (currentflag.equals("1")) {
            for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
                currentList.add(depotOrderDetailBeanList.get(i));
            }
        } else if (currentflag.equals("2")) {
            for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
                RentOrderDetailBean temp = depotOrderDetailBeanList.get(i);
                if (temp.ifOk.equals("0")) {
                    currentList.add(temp);
                }
            }
        } else {
            for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
                RentOrderDetailBean temp = depotOrderDetailBeanList.get(i);
                if (temp.ifOk.equals("1")) {
                    currentList.add(temp);
                }
            }
        }
        int undo = 0;
        for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
            RentOrderDetailBean temp = depotOrderDetailBeanList.get(i);
            if (temp.ifOk.equals("0")) {
                undo++;
            }
        }
        int done = depotOrderDetailBeanList.size() - undo;
        //更新标题
        bt_all.setText("全部[" + depotOrderDetailBeanList.size() + "]");
        bt_undo.setText("待处理[" + undo + "]");
        bt_done.setText("已处理[" + done + "]");
        //更新数据
        RentDetailAdapter adapter = new RentDetailAdapter(currentList, context);
        lv_data.setAdapter(adapter);
    }

    //查询订单明细
    private void QueryDepotOrder() {
        CommonUtil.showPress(this);
        QueryDepotOrderTask QueryPackInfoTask = new QueryDepotOrderTask(RentOrderDetailActivity.this);
        QueryPackInfoTask.execute();
    }

    //左滑将未处理的配件标记为已处理
    private void DEPOT_04() {

        DepotPartOrderTask DepotPartOrderTask = new DepotPartOrderTask(RentOrderDetailActivity.this);
        DepotPartOrderTask.execute();
    }

    //左滑将已处理的配件标记为未处理
    private void DEPOT_05() {
        DepotUnPartOrderTask DepotUnPartOrderTask = new DepotUnPartOrderTask(RentOrderDetailActivity.this);
        DepotUnPartOrderTask.execute();
    }

    //提交完成
    private void DEPOT_06() {
        CommonUtil.showPress(this);
        CompletePartOrderTask completePartOrderTask = new CompletePartOrderTask(RentOrderDetailActivity.this);
        completePartOrderTask.execute();
    }

    //上传唯一码
    private void DEPOT_08(PartBean partBean) {
        //1、判断是否在列表里面
        boolean found = false;
        RentOrderDetailBean foundBean = null;
        for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
            RentOrderDetailBean depotOrderDetailBean = depotOrderDetailBeanList.get(i);

            // 判断无码件还是有码件 0 无码件 1 有码件
            String ifGyssm = depotOrderDetailBean.ifGyssm;
            //判断是否扫描过
            for (int j = 0; j < depotOrderDetailBean.serialNoList.size(); j++) {
                if (partBean.serialNo.equals(depotOrderDetailBean.serialNoList.get(j).serialNo)) {
                    mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
                    Toast.makeText(RentOrderDetailActivity.this, "不能重复扫描配件", Toast.LENGTH_LONG).show();
                    return;
                }
            }
            if ("1".equals(ifGyssm)) {
                //只有有码件才匹配，匹配规则：配件代码和供应商代码完全一致
                if (depotOrderDetailBean.partNo.equals(partBean.partNo) && depotOrderDetailBean.supplierCode.equals(partBean.supplierCode)) {
                    found = true;
                    foundBean = depotOrderDetailBean;
                    break;
                }
            }
        }
        //2、上传
        if (found) {
            //et_bar.setText("");
            CommonUtil.showPress(this);
            ScanUpdateTask scanUpdateTask = new ScanUpdateTask(RentOrderDetailActivity.this, foundBean.dtlId, partBean.serialNo, partBean.supplierCode);
            scanUpdateTask.execute();
        } else {
            mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
            Toast.makeText(this, "扫描的配件不在待扫列表中", Toast.LENGTH_LONG).show();
        }
    }

    //查询订单明细服务
    class QueryDepotOrderTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        QueryDepotOrderTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                //设置当前最后操作的用户
                RentOperator operator = new RentOperator();
                operator.operatorName = SysParameter.userInfo.userName;
                msg = WebServiceHelper_Rent.RENT_05(orderBean.borrowId,operator);
                if (msg.contains("成功")) {
                    depotOrderDetailBeanList = WebServiceHelper_Rent.RENT_03(orderBean.borrowId);
                    msg = "成功";
                }

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //左滑一键处理完当前配件服务
    class DepotPartOrderTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        DepotPartOrderTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Rent.RENT_06(currentDetailBean.dtlId);
                if (msg.contains("成功")) {
                    depotOrderDetailBeanList = WebServiceHelper_Rent.RENT_03(orderBean.borrowId);
                }

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            if (msg.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //左滑一键取消当前配件服务
    class DepotUnPartOrderTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        DepotUnPartOrderTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Rent.RENT_09(currentDetailBean.dtlId);
                if (msg.contains("成功")) {
                    depotOrderDetailBeanList = WebServiceHelper_Rent.RENT_03(orderBean.borrowId);
                }

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            if (msg.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //上传扫码明细服务
    class ScanUpdateTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String dtlId;
        private String serialNo;
        private String supplierCode;

        ScanUpdateTask(Context context, String dtlId, String serialNo, String supplierCode) {
            super();
            mcontext = context;
            this.dtlId = dtlId;
            this.serialNo = serialNo;
            this.supplierCode = supplierCode;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Rent.RENT_07(orderBean.borrowId, dtlId, serialNo);
                if (msg.contains("成功")) {
                    depotOrderDetailBeanList = WebServiceHelper_Rent.RENT_03(orderBean.borrowId);
                }

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                //查当前扫描配件的状态
                boolean currentPartOk = false;
                for (int i = 0; i < depotOrderDetailBeanList.size(); i++) {
                    if (depotOrderDetailBeanList.get(i).dtlId.equals(dtlId)) {
                        if ("1".equals(depotOrderDetailBeanList.get(i).ifOk)) {
                            currentPartOk = true;
                        }
                        break;
                    }
                }
                if (!currentPartOk) {
                    //如果当前配件没完成那么就播放加1
                    mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1);
                } else {
                    //如果当前配件完成就播放完成
                    mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1);
                }
                BindData(mcontext);
            } else {
                mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //全部拣货提交服务
    class CompletePartOrderTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        CompletePartOrderTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Rent.RENT_10(orderBean.borrowId);

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                mSoundPool.play(soundID.get(4), 1, 1, 0, 0, 1);
                finish();
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //获取当前订单最后操作员服务
    class QueryLastOperatorTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        RentOperator operator;

        QueryLastOperatorTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                operator = WebServiceHelper_Rent.RENT_04(orderBean.borrowId);
                msg = "成功";

            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                boolean ok = false;

                if (operator.operatorName == null || "".equals(operator.operatorName)) {
                    //当前订单无人操作
                    ok = true;
                }
                //判断最后操作用户跟当前用户是否一致
                if (SysParameter.userInfo != null && operator.operatorName.equals(SysParameter.userInfo.userName)) {
                    //当前订单是本人
                    ok = true;
                }
                //test
                //ok = true;
                //test
                if (!ok) {
                    //当前订单是其它人操作
                    AlertDialog alertDialog = new AlertDialog.Builder(RentOrderDetailActivity.this)
                            //.setTitle("这是标题")
                            .setMessage("当前订单的最后操作员是" + operator.operatorName + ",是否继续操作?")
                            //.setIcon(R.mipmap.ic_launcher)
                            .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加"Yes"按钮
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    QueryDepotOrder();
                                }
                            })

                            .setNegativeButton("否", new DialogInterface.OnClickListener() {//添加取消
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    finish();
                                }
                            })
                            .create();
                    alertDialog.show();

                } else {
                    //当前订单是本人或者无人操作
                    QueryDepotOrder();
                }
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //region 以下为音频播放
    private SoundPool mSoundPool = null;
    private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();

    private void initSoundPool() {
        //设置最多可容纳4个音频流，音频的品质为5
        mSoundPool = new SoundPool(4, AudioManager.STREAM_SYSTEM, 5);
        soundID.put(1, mSoundPool.load(this, R.raw.right, 1));
        soundID.put(2, mSoundPool.load(this, R.raw.error, 1));
        soundID.put(3, mSoundPool.load(this, R.raw.addone, 1));
        soundID.put(4, mSoundPool.load(this, R.raw.done, 1));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSoundPool != null) {
            mSoundPool.release();
            mSoundPool = null;
        }
    }
    //endregion

    //region 以下为扫码相关

    //初始化
    private void init(){
        //初始化Profile
        ScanHelper.CreateProfile(this);
        //设置广播消息的接收器
        registerReceiver();
    }

    //查询条码服务
    class QueryBarCodeTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";
        private String barCode;
        private BarCodeBean barCodeBean = null;

        QueryBarCodeTask(Context context, String barCode) {
            super();
            mcontext = context;
            this.barCode = barCode;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                barCodeBean = WebServiceHelper.ParseBarCode(barCode);
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                if (barCodeBean.barType == BarCodeBean.BAR_PART) {
                    PartBean partBean = barCodeBean.partBean;
                    if (partBean == null) {
                        mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
                        Toast.makeText(RentOrderDetailActivity.this, "无效条码：" + barCode, Toast.LENGTH_LONG).show();
                    } else {
                        DEPOT_08(partBean);
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "无效配件条码", Toast.LENGTH_LONG).show();
                }

            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //扫码处理
    private void ScanData(String barCode) throws Exception {
        if (barCode != null && barCode.length() > 0) {
            CommonUtil.showPress(this);
            QueryBarCodeTask queryBarCodeTask = new QueryBarCodeTask(RentOrderDetailActivity.this, barCode);
            queryBarCodeTask.execute();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        unRegisterReceiver();
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver();
    }

    //注册广播
    private void registerReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(ScanHelper.ACTION_RESULT_NOTIFICATION);   // for notification result
        filter.addAction(ScanHelper.ACTION_RESULT);                // for error code result
        filter.addCategory(Intent.CATEGORY_DEFAULT);    // needed to get version info
        filter.addAction(ScanHelper.ACTION_SCAN_OUTPUT); // register to received broadcasts via DataWedge scanning
        registerReceiver(mResultReceiver, filter);
    }

    //取消注册广播
    private void unRegisterReceiver() {
        try {
            unregisterReceiver(mResultReceiver);
        } catch (Exception e) {
            Toast.makeText(this, "注销扫码失败：" + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    //监听扫码数据的广播，当设置广播输出时作用该方法获取扫码数据
    private BroadcastReceiver mResultReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ScanHelper.ACTION_SCAN_OUTPUT)) {
                // 处理扫码结果
                try {
                    String decodedData = intent.getStringExtra("com.symbol.datawedge.data_string");
                    et_bar.setText(decodedData);
                    ScanData(decodedData);
                } catch (Exception e) {
                    e.printStackTrace();
                    mSoundPool.play(soundID.get(2), 1, 1, 0, 0, 1);
                    Toast.makeText(RentOrderDetailActivity.this, "扫描失败：" + e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
    };
    //endregion

    class RentDetailAdapter extends BaseAdapter {
        private List<RentOrderDetailBean> depotOrderDetailBeanList;//数据源
        private LayoutInflater mInflater;//布局装载器对象
        private Context mContext;

        public RentDetailAdapter(List<RentOrderDetailBean> depotOrderDetailBeanList, Context context) {
            this.depotOrderDetailBeanList = depotOrderDetailBeanList;
            mInflater = LayoutInflater.from(context);
            mContext = context;
        }

        @Override
        public int getCount() {
            //return返回的是int类型，也就是页面要显示的数量。
            return depotOrderDetailBeanList.size();
        }

        @Override
        public Object getItem(int position) {
            return depotOrderDetailBeanList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //将布局文件转化为View对象
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_rentorderdetail, null);
            }
            //货架
            TextView tv_position = convertView.findViewById(R.id.tv_position);
            //数量
            TextView tv_partCount = convertView.findViewById(R.id.tv_partCount);
            //已拣
            TextView tv_doneCount = convertView.findViewById(R.id.tv_doneCount);
            //待拣
            TextView tv_todoCount = convertView.findViewById(R.id.tv_todoCount);
            //待拣文字
            TextView tv_todoCountTitle = convertView.findViewById(R.id.tv_todoCountTitle);
            //完成
            TextView tv_ok = convertView.findViewById(R.id.tv_ok);
            //图片
            ImageView iv_img = convertView.findViewById(R.id.iv_img);
            //配件信息
            TextView tv_partInfo = convertView.findViewById(R.id.tv_partInfo);
            //供应商信息
            TextView tv_supplierInfo = convertView.findViewById(R.id.tv_supplierInfo);


            //获取相应索引的ItemBean对象
            RentOrderDetailBean bean = depotOrderDetailBeanList.get(position);
            tv_position.setText(bean.position);
            tv_partCount.setText(String.valueOf(bean.partCount));
            tv_doneCount.setText(String.valueOf(bean.pickedCount));
            tv_todoCount.setText(String.valueOf(bean.notPickedCount));
            String ymj = "有码件";
            if( "0".equals(bean.ifGyssm)) {
                ymj = "无码件";
            }
            tv_partInfo.setText(bean.partCode + " " + bean.partName + " " + ymj);
            tv_supplierInfo.setText(bean.supplierCode + " " + bean.supplierName);

            if (bean.ifOk.equals("1")) {
                tv_ok.setVisibility(View.VISIBLE);
                tv_todoCount.setVisibility(View.GONE);
                tv_todoCountTitle.setVisibility(View.GONE);
            } else {
                tv_ok.setVisibility(View.GONE);
                tv_todoCount.setVisibility(View.VISIBLE);
                tv_todoCountTitle.setVisibility(View.VISIBLE);
            }

            if (bean.imgUrl != null && !bean.imgUrl.equals("")) {
                Picasso.with(mContext).load(bean.imgUrl).into(iv_img);
            } else {
                iv_img.setImageDrawable(convertView.getResources().getDrawable(R.drawable.ic_noimage));
            }
            return convertView;
        }
    }
}
